﻿{ VerUtilsHelper.pas

  Copyright (C) 2011 ~ 2013, Takeda Matsuki <matsukitakeda@gmail.com> and others.
  Portion of third party hard works hold by their respective owner.

  This library is free software; you can redistribute it and/or modify it
  under the terms of the GNU Library General Public License as published by
  the Free Software Foundation; either version 2 of the License, or (at your
  option) any later version with the following modification:

  As a special exception, the copyright holders of this library give you
  permission to link this library with independent modules to produce an
  executable, regardless of the license terms of these independent modules,and
  to copy and distribute the resulting executable under terms of your choice,
  provided that you also meet, for each linked independent module, the terms
  and conditions of the license of that module. An independent module is a
  module which is not derived from or based on this library. If you modify
  this library, you may extend this exception to your version of the library,
  but you are not obligated to do so. If you do not wish to do so, delete this
  exception statement from your version.

  This program is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
  for more details.

  You should have received a copy of the GNU Library General Public License
  along with this library; if not, write to the Free Software Foundation,
  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  -----------------------------------------------------------------------------
  
  Version	: 1.0a <2013-07-10 at 4.42 AM GMT+7>
}

unit extVerUtilsHelper;

{$mode objfpc}{$H+}

{$IF FPC_FULLVERSION >= 20700}
 {$CodePage UTF8}
{$ENDIF}


interface

uses
  Classes, SysUtils, resource, versiontypes, versionresource;

type
  { TVersionInfo }

  TVersionInfo = class
  private
    FVersResource: TVersionResource;
    function GetFixedInfo: TVersionFixedInfo;
    function GetStringFileInfo: TVersionStringFileInfo;
    function GetVarFileInfo: TVersionVarFileInfo;
  public
    constructor Create;
    destructor Destroy; override;

    procedure Load(Instance: THandle);
    property FixedInfo: TVersionFixedInfo read GetFixedInfo;
    property StringFileInfo: TVersionStringFileInfo read GetStringFileInfo;
    property VarFileInfo: TVersionVarFileInfo read GetVarFileInfo;
  end;

  {---------------------------------------------------------------
  Description : 
   @extGetStrFileVersion is simply solution to get Version info
   of our release product number.
 ---------------------------------------------------------------}
  function extGetStrFileVersion :String;
  
 {---------------------------------------------------------------
  Description : 
   @extGetStrFileVersionDetail is simply solution to get Version 
   info of our release product number. It's would give you :
   1. Major Number
   2. Minor Number
   3. Revision Number
   4. Build Number
 ---------------------------------------------------------------}
 procedure extGetStrFileVersionDetail (out NumMajor, NumMinor, NumRev, NumBuild :Int64);  

implementation

{ TVersionInfo }

function TVersionInfo.GetFixedInfo: TVersionFixedInfo;
begin
  Result := FVersResource.FixedInfo;
end;

function TVersionInfo.GetStringFileInfo: TVersionStringFileInfo;
begin
  Result := FVersResource.StringFileInfo;
end;

function TVersionInfo.GetVarFileInfo: TVersionVarFileInfo;
begin
  Result := FVersResource.VarFileInfo;
end;

constructor TVersionInfo.Create;
begin
  inherited Create;
  FVersResource := TVersionResource.Create;
end;

destructor TVersionInfo.Destroy;
begin
  FVersResource.Free;
  inherited Destroy;
end;

procedure TVersionInfo.Load(Instance: THandle);
var
  Stream: TResourceStream;
begin
  Stream := TResourceStream.CreateFromID(Instance, 1, PAnsiChar(RT_VERSION));
  try
    FVersResource.SetCustomRawDataStream(Stream);
    // access some property to load from the stream
    FVersResource.FixedInfo;
    // clear the stream
    FVersResource.SetCustomRawDataStream(nil);
  finally
    Stream.Free;
  end;
end;

function extGetStrFileVersion :String;
const
  Fmt: String = '%d.%d.%d.%d';
var
  BuildVerInfo : TVersionInfo;
  NumMajor, NumMinor, NumRev, NumBuild :Int64;
begin
  // Fulfill with all information needed (initialization)
  BuildVerInfo := TVersionInfo.Create;
  BuildVerInfo.Load(HINSTANCE);

  // grab the Build Number
  NumMajor := BuildVerInfo.FixedInfo.FileVersion[0];
  NumMinor := BuildVerInfo.FixedInfo.FileVersion[1];
  NumRev := BuildVerInfo.FixedInfo.FileVersion[2];
  NumBuild := BuildVerInfo.FixedInfo.FileVersion[3];

  // Set Free for BuildVerInfo
  BuildVerInfo.Free;

  Result := Format(Fmt, [NumMajor, NumMinor, NumRev, NumBuild]);
end;

procedure extGetStrFileVersionDetail (out NumMajor, NumMinor, NumRev, NumBuild :Int64);
var
  BuildVerInfo : TVersionInfo;
begin
  // Fulfill with all information needed (initialization)
  BuildVerInfo := TVersionInfo.Create;
  BuildVerInfo.Load(HINSTANCE);

  // grab the Build Number
  NumMajor := BuildVerInfo.FixedInfo.FileVersion[0];
  NumMinor := BuildVerInfo.FixedInfo.FileVersion[1];
  NumRev := BuildVerInfo.FixedInfo.FileVersion[2];
  NumBuild := BuildVerInfo.FixedInfo.FileVersion[3];

  // Set Free for BuildVerInfo
  BuildVerInfo.Free;
end;

end. 
